package dav.mobile.garuda.states.game.threads;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

import dav.mobile.garuda.states.StateGame;

public class ForceThread extends Thread {
	//Attributes
	private long 		m_Time;
	private float 		m_Modifier; 
	private StateGame 	m_Game;
	
	//Constructor
	public ForceThread(StateGame game) {
		//Active in one second
		m_Time = 1000;
		
		//Force modifier
		m_Modifier = 10000.0f;
		
		//StateGame pointer
		m_Game = game;
	}
	
	@Override
	public void run() {
		Vector2 m_TempForce = new Vector2(0,0);
		float oldX = 0, oldY = 0, oldZ = 0, curX = 1, curY = 1, curZ = 1;
		
		//get acc input
		if (Gdx.input.isAccelerometerAvailable()) {
			oldX = Gdx.input.getAccelerometerX();
			oldY = Gdx.input.getAccelerometerY();
			oldZ = Gdx.input.getAccelerometerZ();
		}
		
		try {
			sleep(m_Time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//get acc input
		if (Gdx.input.isAccelerometerAvailable()) {
			curX = Gdx.input.getAccelerometerX();
			curY = Gdx.input.getAccelerometerY();
			curZ = Gdx.input.getAccelerometerZ();
		}
			
		//dumbass calculation, fix this
		Vector3 m_Vector = new Vector3(curX - oldX, curY - oldY, curZ - oldZ);
		m_Vector = m_Vector.mul(2.0f);
		float m_Pos = m_Vector.len();
		m_TempForce = new Vector2(m_Pos * Math.abs(m_Vector.x) * m_Modifier, m_Pos * Math.abs((m_Vector.y + m_Vector.z) / 2) * m_Modifier);
		
		//Set game force
		m_Game.setForce(m_TempForce);
	}
}
